<?php
global $woodb;
$lecture_id = $_REQUEST['lecture_id'];
$course_id = $_REQUEST['course_id' ];
$lecture = ($woodb->getLecture($lecture_id));
$courses = ($woodb->getCourseByID($course_id));
$lecture = $lecture[0];
$course = $courses[0];
$materials = $woodb -> getAllMaterial();
		
switch ($_SERVER['REQUEST_METHOD']) {
	case 'POST':
		$mats = $_POST['mat'];
		if ($mats != null) {
			foreach ($mats as $mat) {
				$woodb->importMaterialTo(intval($lecture_id), intval($mat));
			}
		}
		echo "<script>window.location = '?page=woodle_admin&action=woodle_edit_lecture&course_id=$course->course_id&lecture_id=$lecture->lecture_id'</script>";
		break;
	}
?>
<div class="wrap" id="import_material" >
	<h2>Import Material</h2>
	<div class='woo_breadcrumbs'>
		<a href='?page=woodle_admin'>Courses</a> &raquo; 
		<a href='?page=woodle_admin&action=woodle_edit_course&course_id=<?=$course->course_id?>'><?=($course->name." (".$course->semester." ".$course->year.")")?></a> &raquo; 
		<a href='?page=woodle_admin&action=woodle_edit_lecture&course_id=<?=$course->course_id?>&lecture_id=<?=$lecture->lecture_id?>'><?=$lecture->name?></a> &raquo; 	
		Import Material
	</div>
	<p style='padding-top:20px'></p>
	<form id="import-material" enctype="multipart/form-data" action="?page=woodle_admin&action=woodle_import_material&course_id=<?=$course_id?>&lecture_id=<?=$lecture_id?>" method="POST">
	<table id = "imp_mat" cellspacing="0">
		<thead>
			<tr>
				<th></th>
				<th>Course</th>
				<th>Date</th>
				<th>Lecture</th>
				<th>Material</th>
				<th>Type</th>
			</tr>
		</thead>
		<tfoot>
			<tr><th></th>
				<th>Course</th>
				<th>Date</th>
				<th>Lecture</th>
				<th>Material</th>
				<th>Type</th>
			</tr>
		</tfoot>
		<tbody>
			<?php 
			foreach ($materials as $m) {
		echo "<tr style='cursor: pointer !important;'>
			<td><input type='checkbox' name='mat[]' value='".$m->material_id."' /></td>
			<td align='center'>$m->course_name</td>
			<td align='center'>" . (($m -> semester == "SS") ? "Spring" : "Fall") . " $m->year</td>
			<td align='center'>".$m->lecture_name."</td>
			<td align='center'><a href='$m->guid' target='_blank'>".$m->material_name."</a></td>
			<td align='center'>".$m->type."</td>
			</tr>";
		}
			?>
		</tbody>
	</table>
	<p class="submit">
	<input type="submit" class="button-primary" value="Import Selected Materials" />
	</p>
	</form>
</div>